﻿using System;
using System.IO;
using System.Web.Services;
using System.Xml;
using __ts.Core.Utilities;

namespace PackageStore
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class PackageStore : System.Web.Services.WebService
    {
        [WebMethod]
        public bool put(string apiKey, byte[] package, string id, string version, string desciption, string authors, string size, string md5, out string output)
        {
            output = string.Empty;
            if (apiKey == "e1eb1596ed344c9c9ccb8dd2d73b448c63abc8d746354131413532313339")
            {
                string dir = string.Format("{0}\\{1}", Server.MapPath("~/Update/Jv6az3A_Dfh3-aO5eqc"), DateTime.Now.ToString("yyyyMMdd"));

                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                string filePath = string.Format("{0}\\{1}.7rd", dir, id);

                if (File.Exists(filePath))
                {
                    File.Move(filePath, string.Format("{0}.{1}.del", filePath, DateTime.Now.ToString("yyyyMMddHHmmss")));
                }

                try
                {
                    using (System.IO.StreamWriter objwrite = new System.IO.StreamWriter(filePath))
                    {
                        foreach (byte b in package)
                        {
                            objwrite.BaseStream.WriteByte(b);
                        }
                    }
                }
                catch (Exception e)
                {
                    output = e.Message;
                    return false;
                }

                md5 = md5.ToLower();
                if (Util.GetMd5(filePath).ToLower() != md5)
                {
                    File.Delete(filePath);
                    output = "MD5检验不通过，可能上传的文件不完整，请重试。";
                    return false;
                }

                try
                {

                    string xmlPath = Server.MapPath("~/packageList.xml");
                    XmlDocument doc = new XmlDocument();
                    doc.Load(xmlPath);
                    XmlNode root = doc.SelectSingleNode("packageList");

                    XmlNode del = root.SelectSingleNode("package[id='" + id + "']");
                    if (del != null) root.RemoveChild(del);

                    XmlElement pk = doc.CreateElement("package");

                    XmlElement xid = doc.CreateElement("id");
                    XmlElement xversion = doc.CreateElement("version");
                    XmlElement xdesc = doc.CreateElement("description");
                    XmlElement xauthors = doc.CreateElement("authors");
                    XmlElement xsize = doc.CreateElement("size");
                    XmlElement xmd5 = doc.CreateElement("md5");
                    XmlElement xpublishTime = doc.CreateElement("publishTime");

                    xid.InnerText = id;
                    xversion.InnerText = version;
                    xdesc.InnerText = desciption;
                    xauthors.InnerText = authors;
                    xsize.InnerText = size;
                    xmd5.InnerText = md5;
                    xpublishTime.InnerText = DateTime.Now.ToString("yyyy/MM/dd HH:mm");

                    pk.AppendChild(xid);
                    pk.AppendChild(xversion);
                    pk.AppendChild(xdesc);
                    pk.AppendChild(xauthors);
                    pk.AppendChild(xsize);
                    pk.AppendChild(xmd5);
                    pk.AppendChild(xpublishTime);

                    root.AppendChild(pk);

                    doc.Save(xmlPath);
                }
                catch (Exception e)
                {
                    output = e.Message;
                    return false;
                }
                return true;
            }
            else
            {
                output = "发布失败！apiKey不正确。";
                return false;
            }
        }
    }
}
